﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;



/// <summary>
/// Post请求
/// </summary>
public class IRestful
{
    public static string RestfulAdd(string jsonParam)
    {
        //string url = "http://192.168.22.119:8082/message-web/messagePush/pushInstanceMsg";开发环境调用的Url

        string url = "http://dmsdbj.com/message-web/messagePush/pushInstanceMsg"; //生产环境调用的url；
        //jsonParam = "{\"className\": \"土木\",\"confirmType\": 0,\"eventName\": \"测试\",\"profession\": \"土木\",\"pushEndTime\":\"2018-12-19 10:01:15\",\"pushStartTime\":\"2018-12-19 10:01:15\",\"templateId\": \"5NZ6U2LvmLRhvq61UxsLHyJ0TzveTpXKiTWmy3iFzcQ\",\"userCode\": \"1231231313\",\"userId\": \"Y9FeDLGhn88LE72jKTeRdJ\",\"userName\": \"薛帅行\"}";

        string json = string.Empty;
        //创建restful的请求
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "post";
        request.ContentType = "application/json";

        //创建参数
        string data = jsonParam;
        byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
        request.ContentLength = byteData.Length;

        //以流的形式附加参数
        using (Stream postStream = request.GetRequestStream())
        {
            postStream.Write(byteData, 0, byteData.Length);
        }

        #region
        //    string url = "http://192.168.22.67:8082/message-web/swagger-ui.html#/messagePush表接口/createUsingPOST";
        //private const string UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3";
        //public static string PostData(string url, string data)
        //{
        //    // Convert to bytes
        //    string strReturn = "";
        //    try
        //    {
        //        byte[] obtPostData = Encoding.UTF8.GetBytes(data);
        //        HttpWebRequest orqRequest = (HttpWebRequest)WebRequest.Create(url);
        //        orqRequest.Timeout = 3000;
        //        orqRequest.Method = "POST";
        //        orqRequest.UserAgent = UserAgent;
        //        //orqRequest.Referer = LoginRefererUrl;
        //        orqRequest.ContentType = "application/x-www-form-urlencoded";
        //        orqRequest.ContentLength = obtPostData.Length;
        //        orqRequest.AllowAutoRedirect = false;
        //        // Add post data to request
        //        Stream stream;
        //        using (stream = orqRequest.GetRequestStream())
        //        {
        //            stream.Write(obtPostData, 0, obtPostData.Length);
        //        }
        //        HttpWebResponse orsResponse = (HttpWebResponse)orqRequest.GetResponse();
        //        using (Stream responseStream = orsResponse.GetResponseStream())
        //        {
        //            using (StreamReader streamRead = new StreamReader(responseStream, Encoding.UTF8))
        //            {
        //                strReturn = streamRead.ReadToEnd();
        //            }
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        //throw new Exception(e.Message);
        //    }
        //    return strReturn;
        #endregion
        //接收来自restful的回复
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            //以流的形式读取，返回的就是字符串的json格式
            StreamReader reader = new StreamReader(response.GetResponseStream());
            json = reader.ReadToEnd();
        }
        return json;
    }

    public static string OnlineRestful(string jsonParam)
    {
        //string url = "http://192.168.22.119:8082/message-web/messagePush/pushInstanceMsg";开发环境调用的Url

        //string url = "http://dmsdbj.com/message-web/messagePush/pushInstanceMsg"; //生产环境调用的url；
        //jsonParam = "{\"className\": \"土木\",\"confirmType\": 0,\"eventName\": \"测试\",\"profession\": \"土木\",\"pushEndTime\":\"2018-12-19 10:01:15\",\"pushStartTime\":\"2018-12-19 10:01:15\",\"templateId\": \"5NZ6U2LvmLRhvq61UxsLHyJ0TzveTpXKiTWmy3iFzcQ\",\"userCode\": \"1231231313\",\"userId\": \"Y9FeDLGhn88LE72jKTeRdJ\",\"userName\": \"薛帅行\"}";
       // string url = "http://192.168.22.119:8082/message-web/roomInfo/updateRoomInfo";//开发环境
        string url = "http://dmsdbj.com/message-web/roomInfo/updateRoomInfo";//生产环境

        string json = string.Empty;
        //创建restful的请求
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "post";
        request.ContentType = "application/json";

        //创建参数
        string data = jsonParam;
        byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
        request.ContentLength = byteData.Length;

        //以流的形式附加参数
        using (Stream postStream = request.GetRequestStream())
        {
            postStream.Write(byteData, 0, byteData.Length);
        }

        //接收来自restful的回复
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            //以流的形式读取，返回的就是字符串的json格式
            StreamReader reader = new StreamReader(response.GetResponseStream());
            json = reader.ReadToEnd();
        }
        return json;
    }

    public static string ExamScoreRestful(string jsonParam)
    {
        string url= "http://192.168.22.124:8090/result-web/result/insertResult"; //开发环境
        //string url = "http://tfjybj.com/result-web/result/insertResult";//生产环境

        string json = string.Empty;
        //创建restful的请求
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "post";
        request.ContentType = "application/json";

        //创建参数
        string data = jsonParam;
        byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
        request.ContentLength = byteData.Length;

        //以流的形式附加参数
        using (Stream postStream = request.GetRequestStream())
        {
            postStream.Write(byteData, 0, byteData.Length);
        }

        //接收来自restful的回复
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            //以流的形式读取，返回的就是字符串的json格式
            StreamReader reader = new StreamReader(response.GetResponseStream());
            json = reader.ReadToEnd();
        }
        return json;
    }

    #region RESTful退出的Get请求
    /// <summary>
    /// Get请求，返回json格式字符串
    /// </summary>
    /// <param name="userCode">用户的账号，手机号</param>
    /// <returns></returns>
    public static string RestfulLogout(string userCode)
    {
        string url = "http://192.168.22.52/auth-web/access/logout";
        string json = string.Empty;

        //组合url的get请求
        url += "/" + userCode;

        //创建restful的请求
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "get";
        request.ContentType = "application/json";

        //接收来自restful的回复
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            //以流的形式读取，返回的就是字符串的json格式
            StreamReader reader = new StreamReader(response.GetResponseStream());
            json = reader.ReadToEnd();
        }
        return json;
    }
    #endregion
}

